<?php
/**
 * validators_UserExists
 * 
 * This validator checks an email address against the  database to make sure it's unique
 *
 */
class validators_UserExists extends Zend_Validate_Abstract {
	const DOES_EXIST = "exists";

	/**
	* Holds the names of fields to match
	*
	* @var array
	*/

	protected $_fieldsToMatch = array();

	/**
	* Validation failure message template definitions
	*
	* @var array
	*/
	protected $_messageTemplates = array(
		self::DOES_EXIST    => "User already exists with %value%.  Please enter a different email address.",
	);

	public function __construct() {}

	/**
	* isValid
	*
	* check to see if the field matches exactly.
	*
	* @param string $value - holds the name of the field to validate
	* @param array $context - array of key/value pairs
	* @return boolean
	*/

	public function isValid($value) {
	
		$value = $value;
		$this->_setValue(htmlentities($value));		
		
		$table 	= new tables_UserTable();
		$select = $table->select()->where('email = ?', $value);
		$rows = $table->fetchRow($select);
	
		if($rows === NULL) {
			return true;
		}
		
		$this->_error(self::DOES_EXIST);
		return false;
		
	}
}
?>